﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MyInterBase;
using System.IO;
using MyIni;

namespace cad2013
{
    public partial class IbForm : Form
    {
        DataSet m_dataSet;
        MyIbClass ib;
        public IbForm()
        {
            InitializeComponent();
            m_dataSet = new DataSet();
        }

        private void butOpenFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog openDlg = new OpenFileDialog();
            openDlg.Filter = "gdb|*.gdb";
            if (openDlg.ShowDialog() == DialogResult.OK)
            {
                string sql = "select * from register order by id";
               // string sql = "select * from sysobjects";
                ib = new MyIbClass(openDlg.FileName);
               
                m_dataSet = ib.GetDataSet(m_dataSet, sql);
                this.dataGridView1.DataSource = ib.GetDataTable("kglsy");
                MessageBox.Show("ok");
            }
        }

        private void butKgl_Click(object sender, EventArgs e)
        {
            DataTable kglTable = ib.GetDataTable("kglsy");
            this.dataGridView1.DataSource = kglTable;
        }

        private void butMnl_Click(object sender, EventArgs e)
        {
            DataTable mnlTable = ib.GetDataTable("register");
            this.dataGridView1.DataSource = mnlTable;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DataTable kglTable = ib.GetDataTable("kglsy");
            DataTable mnlTable = ib.GetDataTable("register");
            DataTable stationTable = ib.GetDataTable("station");
            string stationName = stationTable.Rows[0]["station_name"].ToString().Trim();
            string path=Application.StartupPath;
            if (!Directory.Exists(path + "\\" + stationName))
            {
                Directory.CreateDirectory(path + "\\" + stationName);
            }
            string[] files= Directory.GetFiles(path + "\\ini");
            foreach (string str in files)
            {
                int index = str.LastIndexOf("\\");
                string filename = str.Substring(index);
                File.Copy(str, path + "\\" + stationName +filename,true);
            }
            string strSort = "id  asc ";
            DataRow[] rows= mnlTable.Select("id>11000 and id<12000 and name not like '%相位%'",strSort);
            ConstructFile(path + "\\" + stationName + "\\gd.txt", rows, "轨道");
            ConstructKgbFile(path + "\\" + stationName + "\\kgb.txt", rows, "开关板",1);
            rows = kglTable.Select("id>8000 and id<9000", strSort);
            ConstructDcFile(path + "\\" + stationName + "\\dc.txt", rows);
            rows = kglTable.Select("id>2000 and id<3000", strSort);
            ConstructXhjFile(path + "\\" + stationName + "\\xhj.txt", rows);
            rows = mnlTable.Select("id>15000 and id<16000 and name like '%电压%'", strSort);
            ConstructFile(path + "\\" + stationName + "\\dmh.txt", rows, "电码化");
            rows = mnlTable.Select("id>26000 and id<27000 and name like '%电压%'", strSort);
            ConstructFile(path + "\\" + stationName + "\\bzdzl.txt", rows, "半自动");
            MessageBox.Show("ok");
        }

        private void ConstructFile(string filename,DataRow[] rows,string secName)
        {
            IniFile inifile = new IniFile(filename);
            inifile.Load();
            int i=1;
            foreach (DataRow r in rows)
            {
                if (i == 48 && secName == "轨道")
                {
                    inifile.WriteString(secName, i.ToString(), "局部");
                    i++;
                }
                string newName = r["name"].ToString().Trim();
                if (newName.Contains("功出电压"))
                {
                    newName = r["name"].ToString().Trim().Replace("功出电压", "");
                }
                else if (newName.Contains("电压"))
                {
                    newName = r["name"].ToString().Trim().Replace("电压", "");
                }
               
                inifile.WriteString(secName, i.ToString(), newName);
                
                i++;
            }
            inifile.WriteInt(secName, "sum", i - 1);
            inifile.Save();
        }

        private void ConstructKgbFile(string filename, DataRow[] rows, string secName, int secIndex)
        {
            IniFile inifile = new IniFile(filename);
            inifile.Load();
            int i = 0;
            foreach (DataRow r in rows)
            {
                if (++i%49== 0)
                {
                    inifile.WriteString(secName + secIndex.ToString(),"sum", "48");
                    secIndex++;
                    i = 1;
                }
                string newName = r["name"].ToString().Trim();
               
                if (newName.Contains("电压"))
                {
                    newName = r["name"].ToString().Trim().Replace("电压", "");
                }
                inifile.WriteString(secName+secIndex.ToString(), i.ToString(), newName+"J");

            }
            inifile.WriteInt(secName + secIndex.ToString(), "sum", i%48==0?48:i%48);
            inifile.Save();
        }

        private void ConstructDcFile(string filename, DataRow[] rows)
        {
            IniFile inifile = new IniFile(filename);
            inifile.Load();
            int z = 0, j = 0;
            string secName="";
            foreach (DataRow r in rows)
            {
                string name = r["name"].ToString().Trim();
                string dcname = name.Substring(0,name.IndexOf("-"));
                if (name.EndsWith("-1DQJF"))
                {
                    secName = "交流道岔";
                    inifile.WriteString(secName, (++j).ToString(), dcname);
                }
                else if (name.EndsWith("-1DQJ"))
                {
                    secName = "直流道岔";
                    inifile.WriteString(secName, (++z).ToString(), dcname);
                }
                
            }
            inifile.WriteInt("直流道岔", "sum", z);
            inifile.WriteInt("交流道岔", "sum", j);
            inifile.Save();
        }

        private void ConstructXhjFile(string filename, DataRow[] rows)
        {
            IniFile inifile = new IniFile(filename);
            inifile.Load();
            int i = 0;
           // string secName = "";
            List<string> xhjs = new List<string>();
            foreach (DataRow r in rows)
            {
                string name = r["name"].ToString().Trim();
                string xhjname = name.Substring(0, name.IndexOf("-"));
                if (!xhjs.Contains(xhjname))
                {
                    xhjs.Add(xhjname);
                }
              
            }
            foreach (string xhj in xhjs)
            {
                if (xhj.StartsWith("D")) continue;
                inifile.WriteString("其他信号机", (++i).ToString(), xhj);
            }
            inifile.WriteInt("其他信号机","sum",i);
            inifile.Save();
        }
    }
}
